package com.zch.house.service.impl;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zch.house.dao.FlatRepairDao;
import com.zch.house.entity.FlatRepair;
import com.zch.house.service.FlatRepairService;
import com.zch.house.util.XmlSession;

public class FlatRepairServiceImpl extends XmlSession implements FlatRepairService {
	private final int PAGE_SIZE = 20;

	@Override
	public List<FlatRepair> getAllFlatRepairByPage(String area, String estate, int unitNumber, int floor,
			Date startDate, Date endDate, int pageIndex) {
		// 使用map集合来存放参数
		Map<String, Object> map = new HashMap<>();
		map.put("area", area);
		map.put("estate", estate);
		map.put("unitNumber", unitNumber);
		map.put("floor", floor);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		int firstRow = (pageIndex - 1) * PAGE_SIZE + 1;
		int lastRow = pageIndex * PAGE_SIZE;
		map.put("firstRow", firstRow);
		map.put("lastRow", lastRow);
		
		//加载配置文件
		initSession();
		//获得mapper的代理类
		FlatRepairDao dao = session.getMapper(FlatRepairDao.class);
		//调用dao层方法
		List<FlatRepair> list = dao.getAllFlatRepair(map);
		//提交事务并关闭session
		closeSession();
		return list;
	}

	@Override
	public int getTotalCount(String area, String estate, int unitNumber, int floor, Date startDate, Date endDate) {
		// 使用map集合来存放参数
		Map<String, Object> map = new HashMap<>();
		map.put("area", area);
		map.put("estate", estate);
		map.put("unitNumber", unitNumber);
		map.put("floor", floor);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		//加载配置文件
		initSession();
		//获得mapper的代理类
		FlatRepairDao dao = session.getMapper(FlatRepairDao.class);
		//调用dao层方法,获取总数据条数
		int counts = dao.getTotalCount(map);
		//提交事务并关闭session对象
		closeSession();
		// 计算总页数
		int totalPages = counts % PAGE_SIZE == 0 ? counts / PAGE_SIZE : counts / PAGE_SIZE + 1;
		return totalPages;
	}

	@Override
	public FlatRepair getFlatRepairById(int h_id) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		FlatRepairDao dao = session.getMapper(FlatRepairDao.class);
		//调用dao层方法
		FlatRepair flatRepair = dao.getFlatRepairById(h_id);
		//提交事务并关闭session
		closeSession();
		return flatRepair;
	}

	@Override
	public boolean addFlatRepair(FlatRepair flatRepair) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		FlatRepairDao dao = session.getMapper(FlatRepairDao.class);
		//调用dao层方法
		boolean flag = dao.addFlatRepair(flatRepair);
		//提交事务并关闭session
		closeSession();
		return flag;
	}

	@Override
	public boolean deleteFlatRepair(int h_id) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		FlatRepairDao dao = session.getMapper(FlatRepairDao.class);
		//调用dao层方法
		boolean flag = dao.deleteFlatRepair(h_id);
		//提交事务并关闭session
		closeSession();
		return flag;
	}

	@Override
	public boolean updateFlatRepair(FlatRepair flatRepair) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		FlatRepairDao dao = session.getMapper(FlatRepairDao.class);
		//调用dao层方法
		boolean flag = dao.updateFlatRepair(flatRepair);
		//提交事务并关闭session
		closeSession();
		return flag;
	}

}
